package com.zhiyun.service.impl;

import com.zhiyun.domain.Course;
import com.zhiyun.domain.Signup;
import com.zhiyun.mapper.SignupMapper;
import com.zhiyun.service.SignupService;
import com.zhiyun.util.Db;
import com.zhiyun.vo.Result;

import java.util.*;

/**
 * @author zc
 * @Date 2025/4/29 下午2:36
 */
public class SignupServiceImpl implements SignupService {
    SignupMapper signupMapper = Db.getMapper(SignupMapper.class);
    @Override
    public Result mySignup(int courseId, int studentId) {
       List<Signup> list =  signupMapper.selectByCourseAndStudentID(courseId,studentId);
        return Result.ok(list);
    }

    @Override
    public Result signUp(int id, String signupCode) {
        Signup signup = signupMapper.selectByPrimaryKey(id);
        if (signup == null) {
            return Result.fail("签到不存在");
        }
        if (signup.getSignupCode().equals(signupCode)){
            signup.setIsSignup(1);
            signup.setSignupTime(new Date());
            signupMapper.updateByPrimaryKey(signup);
            return Result.okMsg("签到成功");
        }else {
            return Result.fail("签到码不正确");
        }
    }
    @Override
    public Result SignupByCourseId(Integer courseId) {
        List<Signup> signups = signupMapper.selectByCourseId(courseId);
        HashSet<Date> dates = new HashSet<>();
        for (Signup signup : signups) {
            dates.add(signup.getDate());
        }
        ArrayList<Date> list = new ArrayList<>(dates);
        Collections.sort(list, new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                return o1.compareTo(o2);
            }
        });
        return Result.ok(list);
    }

    @Override
    public Result selectSignup(Date date, int courseid) {
       List<Signup> signups =  signupMapper.selectByDateAndCourseId(date,courseid);
        return Result.ok(signups);
    }

    @Override
    public Result reSignup(int signupId) {
        Signup signup = signupMapper.selectByPrimaryKey(signupId);
        if (signup == null) {
            return Result.fail("签到不存在");
        }
        if (signup.getIsSignup() == 1){
            return Result.fail("该学生已签到");
        }
        signup.setIsSignup(1);
        signup.setSignupTime(new Date());
        int rows = signupMapper.updateByPrimaryKey(signup);
        if (rows > 0){
            return Result.okMsg("补签成功");
        }
        return Result.fail("补签失败");
    }

    @Override
    public Result insert(List<Signup> list) {
        StringBuilder code = new StringBuilder();
        try {
            Db.startTransaction();
            char[] chars = "1234567890".toCharArray();
            for (int i = 0; i < 4; i++) {
                code.append(chars[(int) (Math.random() * chars.length)]);
            }
            for (Signup signup : list) {
                signup.setSignupCode(code.toString());
                Signup oldSignUp = signupMapper.selectByCourseAndStudentIDAndDate(signup.getCourseId(), signup.getStudentId(), signup.getDate());
                if (oldSignUp != null) {
                    throw new Exception("插入失败");
                }
                int rows = signupMapper.insert(signup);
                if (rows == 0) {
                    throw new Exception("插入失败");
                }
            }
            Db.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            Db.rollbackTransaction();
            return Result.fail("发布失败");
        } finally {
            Db.close();
        }
        return Result.ok("发布成功", code.toString());
    }
}
